<?php defined('IN_LK') or die('Access Denied');
lk::lo_app('index_common', 'index', 0);
class content extends index_common
{
	private $content_db = null;
	private $member_db  = null;
	
	public function __construct()
	{
		parent::__construct();
		$this->content_db = lk::load_model('content');
		$this->member_db  = lk::load_model('member');
	}
	
	/**
	 * 取最近浏览
	 */
	public function get_view_info()
	{
		$not = isset($_GET['n']) ? (int)$_GET['n'] : 0;
		$ids = parent::get_views();
		if(!$ids) : exit(''); endif;
		$id = implode("', '", $ids);
		unset($ids);
		$info = $this->content_db->get('title,id', "`id` IN ('$id')");
		$str = '';
		if($info)
		{
			foreach($info as $p)
			{
				if($p['id'] == $not)
				{
					continue;
				}
				$str .= '<dd><a href="?m=index&c=show&id=' . $p['id'] . '">' . $p['title'] . '</a></dd>';
			}
		}
		echo format_js($str);
		exit;
	}
	
	/**
	 * 公告
	 */
	public function announ()
	{
		$info = $this->content_db->listinfo('title,id', "`catid`='3' AND `posids` LIKE '%4%' AND `status`='1'", 0, 6, '`id` DESC');
		ob_start();
		include template('announ_list');
		$data = ob_get_clean();
		echo format_js($data);
		exit;
	}
	
	/**
	 * 修改删除信息页面
	 */
	public function e()
	{
		parent::chk_login_jump();
		$page    = isset($_GET['page']) ? (int)$_GET['page'] : 1;
		$data = $this->content_db->listinfo('*', "`userid`='" . $_SESSION['_userid'] . "' AND `status`='1'", $page, 20, '`id` DESC');
		$page_nav = $this->content_db->page_nav;
		if(is_array($data) && $data)
		{
			foreach($data as $ddk => $ddv)
			{
				$data[$ddk]['updatetime'] = date('Y-m-d H:i', $ddv['updatetime']);
			}
		}
		unset($ddv, $ddk);
		include template('my_list');
	}
	
	/**
	 * 修改动作
	 */
	public function edit()
	{
		if(isset($_POST['dosubmit']))
		{
			$info = isset($_POST['info']) ? $_POST['info'] : array();
			$id   = isset($_POST['id']) ? $_POST['id'] : ''; 
			if(!$info['c']['man'] || !$info['c']['tel'] || !$info['c']['addr'] 
			   || !$info['content'] || !$info['title'] || !$info['objecttype'] || !$info['pxfangshi'])
			{
				showmsg(LANG('parameters_error'), false);
			}
			$len = strlen($info['title']);
			if($len <2 || $len> 80)
			{
				showmsg(LANG('title_2_80'), false);
			}
			$info['info'] = new_addslashes(var_export($info['c'], true));
			$fields = array('catid', 'title', 'objecttype', 'pxfangshi', 'content', 'info', 'price');
			$info = in_fields($info, $fields);
			$info['updatetime'] = TIME;
			if(!$this->content_db->is_myinfo($_SESSION['_userid'], $id))
			{
				showmsg(LANG('parameters_error'), false);
			}
			$r = $this->content_db->update($info, "`id`='$id'");
			if($r)
			{
				showmsg(LANG('edit_info_success'), true, '?m=index&c=content&a=e');
			}
		}
		else
		{
			$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
			if(!$id || !$this->content_db->is_myinfo($_SESSION['_userid'], $id))
			{
				showmsg(LANG('parameters_error'), false);
			}
			$cats  = get_cache('categorys');
			$c  = $this->content_db->is_exists($id);
			$catname = $cats[$c['catid']]['catname'];
			$objs  = $this->content_db->objs;
			$pxfs  = $this->content_db->pxfs;
			lk::lo('form', '', 0);
			$select  = form::select($objs, 'id="objecttype" name="info[objecttype]"', $c['objecttype']);
			eval("\$info=" . $c['info'] . ";");
			$man  = isset($info['man']) ? $info['man'] : '';
			$tel  = $info['tel'];
			$addr = $info['addr'];
			include template('pub_edit');
		}
	}
	
	/**
	 * 删除动作
	 */
	public function del()
	{
		$id = isset($_GET['id']) ? (int)$_GET['id'] : exit('0');
		if($this->content_db->is_myinfo($_SESSION['_userid'], $id))
		{
			$this->content_db->del_info($id) ? exit('1') : exit('0');
		}
		exit('0');
	}
	
	/**
	 * 发布页
	 */
	public function pub()
	{
		$ac    = isset($_GET['catid']) && $_GET['catid'] ? 1 : 0;
		if($ac)
		{
			$catid = isset($_GET['catid']) && $_GET['catid'] ? (int)$_GET['catid'] : 0;
			$cats  = get_cache('categorys');
			$field = array(1, 2);  //目前只允许这两种分类
			$objs  = $this->content_db->objs;
			$pxfs  = $this->content_db->pxfs;
			if(!in_array($catid, $field))
			{
				showmsg(LANG('parameters_error'), false);
			}
			$catname = $cats[$catid]['catname'];
		}
		include template('pub');
	}
	
	/**
	 * 发布动作
	 */
	public function dopub()
	{
		if(isset($_POST['dosubmit']))
		{
			$info = $_POST['info'];
			if(isset($info['login']['username']) && $info['login']['username'] &&
			isset($info['login']['password']) && $info['login']['password'])
			{
				$pwd = $this->member_db->login_check($info['login']['username'], $info['login']['password']);
				$this->member_db->dologin($info['login']['username'], $pwd);
			}
			else if(isset($info['reg']['username']) && isset($info['reg']['password1']) && 
			isset($info['reg']['password2']) && isset($info['reg']['email']) && 
			$info['reg']['username'] && $info['reg']['password1'] && $info['reg']['email'] && 
			$info['reg']['password2'])
			{
				$info['reg']['nickname'] = $info['reg']['username'];
				$this->member_db->doregister($info['reg']);
			}
			if(!isset($_SESSION['_username']) || !$_SESSION['_username'])
			{
				showmsg(LANG('parameters_error'), false);
			}
			if(!$this->content_db->not_allowpost())
			{
				showmsg(LANG('you_postmax'), false);
			}
			if(!$info['c']['man'] || !$info['c']['tel'] || !$info['c']['addr'] 
			   || !$info['content'] || !$info['title'] || !$info['objecttype'] || !$info['pxfangshi'])
			{
				showmsg(LANG('parameters_error'), false);
			}
			$len = strlen($info['title']);
			if($len <2 || $len> 80)
			{
				showmsg(LANG('title_2_80'), false);
			}
			$info['info'] = new_addslashes(var_export($info['c'], true));
			$fields = array('catid', 'title', 'objecttype', 'pxfangshi', 'content', 'info', 'price');
			$info = in_fields($info, $fields);
			$info['allow_comment'] = $info['status'] = 1;
			$info['username']   = $_SESSION['_username'];
			$info['userid']     = $_SESSION['_userid'];
			$info['updatetime'] = $info['inputtime'] = TIME;
			$r = $this->content_db->insert($info);
			if($r)
			{
				$_SESSION['gang_id'] = $r;
				header('Location:?m=index&c=content&a=pub_success');
			}
		}
		else
		{
			showmsg(LANG('parameters_error'), false);
		}
	}
	
	/**
	 * 发布成功页面
	 */
	public function pub_success()
	{
		$id = isset($_SESSION['gang_id']) ? $_SESSION['gang_id'] : header('Location:?m=index');
		unset($_SESSION['gang_id']);
		include template('pub_success');
	}
}